package com.epalmpay.controller.channel.agentmanage;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.agentmanage.TradeWithdrawListInDTO;
import com.epalmpay.dto.agentmanage.TradeWithdrawListOutDTO;
import com.epalmpay.dto.agentmanage.TradeWithdrawOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.TradeWithdraw;
import com.epalmpay.service.withdraw.ITradeWithdrawService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

/**
 * Created by Administrator on 2017/6/6.
 */
@Controller
@RequestMapping(value="/channel/withdraw")
public class TradeWithdrawController extends BaseController{
    @Resource
    ITradeWithdrawService tradeWithdrawService;


    /**
     * 提现，数据列表
     *
     */
    @RequestMapping(value="/tradeWithdrawList")
    @ResponseBody
    public TradeWithdrawListOutDTO TradeWithdrawList(TradeWithdraw tradeWithdraw, PageDTO pageDTO){
        TradeWithdrawListOutDTO tradeWithdrawListOutDTO = new TradeWithdrawListOutDTO();
        try {

            Page page= Tools.pageEmtity(pageDTO);

            //查询时取数据
            if(tradeWithdraw.getIsReload()){
                //获取统计设备信息
                List<TradeWithdrawOutDTO> tradeWithdrawList = tradeWithdrawService.getTradeWithdrawList(tradeWithdraw, page);
                tradeWithdrawListOutDTO.setTradeWithdrawOutDTOList(tradeWithdrawList);
            }
            tradeWithdrawListOutDTO.setPage(page);
            tradeWithdrawListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return tradeWithdrawListOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawListOutDTO;
    }


    /**
     * 提现，查询详情
     *
     */
    @RequestMapping(value="/getDetailsById")
    @ResponseBody
    public TradeWithdrawOutDTO GetDetailsById(TradeWithdraw tradeWithdraw){
        TradeWithdrawOutDTO tradeWithdrawOutDTO = new TradeWithdrawOutDTO();
        try {





            //获取统计设备信息
            TradeWithdraw tradeWithdraw1 = tradeWithdrawService.GetTradeWithdrawById(tradeWithdraw.getId());
            PropertyCopyUtil.copyProperties(tradeWithdraw1,tradeWithdrawOutDTO);



            tradeWithdrawOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return tradeWithdrawOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawOutDTO;
    }


    /**
     * 提现，更新单笔数据
     *
     */
    @RequestMapping(value="/updateDetailsById")
    @ResponseBody
    public TradeWithdrawOutDTO UpdateDetailsById(TradeWithdraw tradeWithdraw,HttpServletRequest request){
        TradeWithdrawOutDTO tradeWithdrawOutDTO = new TradeWithdrawOutDTO();
        try {





            //获取统计设备信息
            int status = tradeWithdrawService.UpdateTradeWithdrawById(tradeWithdraw,request);



            if (status>0){
                tradeWithdrawOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            }else{
                tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
            }

            return tradeWithdrawOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawOutDTO;
    }


    /**
     * 提现，批量更新数据
     *
     */
    @RequestMapping(value="/batchUpdateDetailsById")
    @ResponseBody
    public TradeWithdrawOutDTO BatchUpdateDetailsById(TradeWithdraw tradeWithdraw,HttpServletRequest request){
        TradeWithdrawOutDTO tradeWithdrawOutDTO = new TradeWithdrawOutDTO();
        try {





            //获取统计设备信息
            int status = tradeWithdrawService.BatchUpdateTradeWithdrawById(tradeWithdraw,request);



            if (status>0){
                tradeWithdrawOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            }else{
                tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
            }

            return tradeWithdrawOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawOutDTO;
    }

    /**
     * 提现，更新批量数据
     *
     */
    @RequestMapping(value="/updateWithdrawByList")
    @ResponseBody
    public TradeWithdrawOutDTO UpdateWithdrawByList(TradeWithdrawListInDTO tradeWithdrawListInDTO){
        TradeWithdrawOutDTO tradeWithdrawOutDTO = new TradeWithdrawOutDTO();
        try {



                int status = tradeWithdrawService.UpdateTradeWithdrawByList(tradeWithdrawListInDTO);
                if (status>0){
                    tradeWithdrawOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
                }else{
                    tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
                }




            return tradeWithdrawOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawOutDTO;
    }



    /**
     * 提现，门店或代理商数据列表
     *
     */
    @RequestMapping(value="/getWithdrawListByAgentIdOrShopId")
    @ResponseBody
    public TradeWithdrawListOutDTO GetWithdrawListByAgentIdOrShopId(TradeWithdraw tradeWithdraw, PageDTO pageDTO){
        TradeWithdrawListOutDTO tradeWithdrawListOutDTO = new TradeWithdrawListOutDTO();
        try {

            Page page= Tools.pageEmtity(pageDTO);

            //查询时取数据
            if(tradeWithdraw.getIsReload()){
                //获取统计设备信息
                List<TradeWithdrawOutDTO> tradeWithdrawList = tradeWithdrawService.GetWithdrawListByAgentIdOrShopId(tradeWithdraw, page);
                tradeWithdrawListOutDTO.setTradeWithdrawOutDTOList(tradeWithdrawList);
            }
//            tradeWithdrawListOutDTO.setPage(page);
            tradeWithdrawListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return tradeWithdrawListOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawListOutDTO;
    }





    /**
     * 提现，更新单笔数据
     *
     */
    @RequestMapping(value="/applyWithdrawById")
    @ResponseBody
    public TradeWithdrawOutDTO ApplyWithdrawById(TradeWithdraw tradeWithdraw){
        TradeWithdrawOutDTO tradeWithdrawOutDTO = new TradeWithdrawOutDTO();
        try {





            //获取统计设备信息
            int status = tradeWithdrawService.ApplyWithdrawById(tradeWithdraw);



            if (status>0){
                tradeWithdrawOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            }else{
                tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
            }

            return tradeWithdrawOutDTO;
        } catch (ApplicationException ae) {
            tradeWithdrawOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
            logger.error(ae);
        }
        catch (Exception e) {
            tradeWithdrawOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
            logger.error(e);
        }
        return tradeWithdrawOutDTO;
    }

}

